package com.example.audiostreaming.adapters;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.audiostreaming.R;
import com.example.audiostreaming.views.ViewCircleImageView;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

public class AdapterPlayerTrackViewPager extends PagerAdapter {
    private List<Track> mListData = new ArrayList<>();

    @Override
    public int getCount() {
        return mListData.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View itemView = LayoutInflater.from(container.getContext()).inflate(R.layout.activity_player_track_view_pager_item, container, false);
        container.addView(itemView);
        //设置数据
        ViewCircleImageView imageView = itemView.findViewById(R.id.player_track_view_pager_item);
        Track track = mListData.get(position);
        String coverUrlLarge = track.getCoverUrlLarge();
        if (!TextUtils.isEmpty(coverUrlLarge)) {
            Picasso.with(container.getContext()).load(coverUrlLarge).into(imageView);
        }else {
            imageView.setImageResource(R.mipmap.logo_icon);
        }

        //旋转动画
        Animation animation = AnimationUtils.loadAnimation(container.getContext(), R.anim.img_animation);
        LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
        animation.setInterpolator(lin);
        imageView.startAnimation(animation);


        return itemView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }





    public void setData(List<Track> list) {
        mListData.clear();
        mListData.addAll(list);
        notifyDataSetChanged();
    }
}
